
import UIKit

class XSWeclomeController: UIViewController {
    
    var bottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.setupView()
        
        self.setupUserInfor()
    }
    
    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            self.weclomeText.alpha = 0;
            
            }) { (_) -> Void in
              
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    
                    self.weclomeText.alpha = 1
                    self.weclomeText.hidden = false
                    
                    }, completion: { (_) -> Void in
                        
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchVc(XSTabBarViewController() as UIViewController)
                })
        }
    }
    
    private func setupView()
    {
        self.view.addSubview(backgroundImage)
        self.view.addSubview(iconView)
        self.view.addSubview(weclomeText)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        weclomeText.translatesAutoresizingMaskIntoConstraints = false
        backgroundImage.translatesAutoresizingMaskIntoConstraints = false
        
        // backgroundImage 约束
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        // weclomeText 约束
        bottomConstraint = NSLayoutConstraint(item: weclomeText, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(bottomConstraint!)
        self.view.addConstraint(NSLayoutConstraint(item: weclomeText, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // iconView 约束
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: weclomeText, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: -16))
    }
    
    private func setupUserInfor()
    {
        
    }
    
    // MARK: - 懒加载
    private lazy var iconView: UIImageView =
    {
        let iconView = UIImageView()
        
        iconView.image = UIImage(named: "avatar_default_big")
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    
    private lazy var weclomeText: UILabel =
    {
        let weclomeText = UILabel()
        
        weclomeText.text = "欢迎回来"
        weclomeText.font = UIFont.systemFontOfSize(15)
        weclomeText.textColor = UIColor.blackColor()
        weclomeText.hidden = true
        
        return weclomeText
    }()
    
    private lazy var backgroundImage: UIImageView =
    {
        let backgroundImage = UIImageView()
        
        backgroundImage.image = UIImage(named: "ad_background")
        
        return backgroundImage
    }()
}
